package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.User;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.PropertyUtil;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsLibraryPosition;
import com.jy.wms.pojo.WmsShelves;
import com.jy.wms.service.WmsShelvesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsShelvesController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author yjs
 * @Date Tue Sep 26 18:23:42 CST 2017
 */
@Controller
@RequestMapping("/wmsshelves")
@Api(value = "/wmsshelves", description = "关于上架管理操作")
public class WmsShelvesController extends BaseController<WmsShelvesService,WmsShelves> {
    /**
     * 上架管理分页查询
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value="/list")
    @ApiOperation(value = "上架管理 POST请求查询上架信息",response = JSONObject.class, notes = "POST请求查询上架信息")
    public @ResponseBody JSONObject getWmsShelves(@RequestBody WmsShelves entity, HttpSession session) {
        List<WmsShelves> list = new ArrayList<>();
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckmc= String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            entity.setCkid(ckid);
            String owner=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
                entity.setHzid(owner);
                list = baseService.baseSelectPage(entity, ckmc);
            }
            Integer size = list.size();
            if (list.size() > 0) {
                //开始的数量
                Integer start = entity.getRows() * (entity.getPage() - 1);
                //结束的数量
                Integer end = entity.getRows() * entity.getPage();
                if (end > list.size()) {
                    end = list.size();
                }
                list = list.subList(start, end);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    , size);
        }catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 上架管理分页查询
     * @param entity
     * @param session
     * @return
     */
    /*@PostMapping(value="/list")
    @ApiOperation(value = "上架管理 POST请求查询上架信息",response = JSONObject.class, notes = "POST请求查询上架信息")
    public @ResponseBody JSONObject getWmsShelves(@RequestBody WmsShelves entity,HttpSession session) {
        Page<WmsShelves> list = new Page<>();
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckmc=String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            entity.setCkid(ckid);
            String owner=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
                entity.setHzid(owner);
                entity.setHzmc(hz.get(0).getShow_hzid());
                list = baseService.baseSelectPage(entity, ckmc);
            }
            *//*Integer size=list.size();
            if (list.size() > 0) {
                //开始的数量
                Integer start = entity.getRows() * (entity.getPage() - 1);
                //结束的数量
                Integer end = entity.getRows() * entity.getPage();
                if (end > list.size()) {
                    end = list.size();
                }
                list = list.subList(start, end);
            }*//*
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    , (int) list.getTotal());
        }catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }*/
    /**
     * 模糊查询ASN编号
     * @param map
     * @param session
     * @return
     */
    @PostMapping(value = "/searchAsnbh")
    @ApiOperation(value = "上架管理 查询ASN编号",response = JSONObject.class, notes = "POST查询输入{\"asnbh\":\"ASN编号模糊查询\"}")
    public @ResponseBody JSONArray searchAsnbh(@RequestBody Map<String,String> map, HttpSession session) {
        JSONArray jsonArray = new JSONArray();
        List<WmsShelves> list = new ArrayList<WmsShelves>();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            list = baseService.searchAsnbh(map.get("asnbh"),ckid);
            for (int i = 0; i < list.size(); i++) {
                WmsShelves wmsShelves =  list.get(i);
                JSONObject json = getJSONObject();
                json.put("dictionaryid",wmsShelves.getAsnbh());
                json.put("value",wmsShelves.getAsnbh());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    /**
     * 模糊查询关联PO编号
     * @param map
     * @param session
     * @return
     */
    @PostMapping(value = "/searchPObh")
    @ApiOperation(value = "上架管理 查询关联PO编号",response = JSONObject.class, notes = "POST查询输入{\"dycgdbh\":\"关联PO编号模糊查询\"}")
    public @ResponseBody JSONArray searchPObh(@RequestBody Map<String,String> map, HttpSession session) {
        JSONArray jsonArray = new JSONArray();
        List<WmsShelves> list = new ArrayList<>();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String owner=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
            }
            list = baseService.searchPObh(map.get("dycgdbh"),ckid,owner);
            for (int i = 0; i < list.size(); i++) {
                WmsShelves wmsShelves =  list.get(i);
                JSONObject json = getJSONObject();
                json.put("dictionaryid",wmsShelves.getDycgdbh());
                json.put("value",wmsShelves.getDycgdbh());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    /**
     * 模糊查询供应商
     * @param wmsShelves
     * @param session
     * @return
     */
    @PostMapping(value = "/searchGysid")
    @ApiOperation(value = "上架管理 查询供应商",response = JSONObject.class, notes = "查询输入{\"show_gysid\":\"供应商模糊查询\"}")
    public @ResponseBody
    JSONArray searchGysid(@RequestBody WmsShelves wmsShelves, HttpSession session) {
        JSONArray jsonArray =new JSONArray();
        Page<WmsShelves> list = new Page<WmsShelves>();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String owner=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
            }
            //查询当前登录用户货主下面的供应商
            list = baseService.searchGysid(wmsShelves, ckid, owner);
            for (WmsShelves wmsShelves2 : list) {
                JSONObject json = getJSONObject();
                json.put("dictionaryid", wmsShelves2.getGysid());
                json.put("value", wmsShelves2.getShow_gysid());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    /**
     * 收货明细页面查询==>上架确认页面
     * 根据预期收货单ID,查询收货表信息
     * @param wmsShelves
     * @param session
     * @return
     */
    @PostMapping(value="/baseSelectDetail/{yqdhbdid}")
    @ApiOperation(value = "上架管理 查询收货信息",response = JSONObject.class, notes = "POST查询输入{\"yqdhbdid\":\"预期到货单表,查询收货信息\"}")
    public @ResponseBody
    JSONObject baseSelectDlistetail(@RequestBody WmsShelves wmsShelves, @PathVariable("yqdhbdid") String yqdhbdid, HttpSession session) {
        Page<WmsShelves> list = new Page<WmsShelves>();
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String hzid = null;//取当前货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                wmsShelves.setYqdhbdid(yqdhbdid);
                list = baseService.baseSelectDetail(wmsShelves, ckid, hzid);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        }catch(ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "查询收货信息失败", e.getMessage(), "");
        }catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查询收货信息失败","","");
        }
        return json;
    }
    /**
     * 上架任务专用
     * 收货明细页面查询==>上架确认页面
     * 根据预期收货单ID,查询收货表信息
     * @param wmsShelves
     * @param session
     * @return
     */
    @PostMapping(value="/baseSelectRw/{yqdhbdid}")
    @ApiOperation(value = "上架任务 查询收货信息",response = JSONObject.class, notes = "POST查询输入{\"yqdhbdid\":\"预期到货单表,查询收货信息\"}")
    public @ResponseBody JSONObject baseSelectRw(@RequestBody WmsShelves wmsShelves, @PathVariable("yqdhbdid") String yqdhbdid, HttpSession session) {
        Page<WmsShelves> list = new Page<WmsShelves>();
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String hzid = null;//取当前货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                wmsShelves.setYqdhbdid(yqdhbdid);
                list = baseService.baseSelectRw(wmsShelves, ckid, hzid);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        }catch(ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "查询收货信息", e.getMessage(), "");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "查询收货信息失败", e.getMessage(), "");
        }
        return json;
    }
    /**
     * 库位模糊查询
     * @param map
     * @param session
     * @return
     */
    @PostMapping(value="/searchKw")
    @ApiOperation(value = "上架管理 库位模糊查询",response = JSONObject.class, notes = "查询输入{\"show_kwid\":\"库位模糊查询\"}")
    public @ResponseBody JSONArray searchKw(@RequestBody Map<String,String> map, HttpSession session) {
        JSONArray jsonArray =new JSONArray();
        List<WmsLibraryPosition> list = new ArrayList<>();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String hzid=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                hzid=hz.get(0).getHzid();
            }
            list = baseService.searchKw(map.get("show_kwid"),ckid,hzid);
            for (int i = 0; i < list.size(); i++) {
                WmsLibraryPosition wmsLibraryPosition =  list.get(i);
                JSONObject json = getJSONObject();
                json.put("dictionaryid",wmsLibraryPosition.getId());
                json.put("value",wmsLibraryPosition.getMc());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    /**
     * 上架管理 新增上架信息
     * @param list
     * @param session
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "上架管理 新增上架信息",response = JSONObject.class, notes = "POST新增上架信息")
    public @ResponseBody JSONObject addWmsLibraryPosition(@RequestBody List<WmsShelves> list, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String hzid=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                hzid=hz.get(0).getHzid();
            }
            baseService.baseInsert(list,ckid,hzid,"1");
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增上架信息成功","", JSONArray.fromObject(list).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"新增上架信息失败",e.getMessage(),"");
        } catch (RuntimeException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "新增上架信息失败", e.getMessage(), "");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "新增上架信息失败", e.getMessage(), "");
        }
        return json;
    }

    /**
     * 操作人模糊查询
     * @param map
     * @param session
     * @return
     */
    @PostMapping(value="/searchCzr")
    @ApiOperation(value = "上架管理 操作人模糊查询",response = JSONObject.class, notes = "查询输入{\"czr\":\"操作人模糊查询\"}")
    public @ResponseBody JSONArray searchCzr(@RequestBody Map<String,String> map, HttpSession session) {
        JSONObject json_s = new JSONObject();
        JSONArray jsonArray=new JSONArray();
        try {
            String hzid=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                hzid=hz.get(0).getHzid();
            }
            json_s.put("code","hz");
            json_s.put("dvalue",hzid);
            json_s.put("userAccount",map.get("czr").toString());
            String host = PropertyUtil.getProperty("safety.host");
            String port = PropertyUtil.getProperty("safety.port");
            String result = HttpUtil.doPost("http://"+host+":"+port+"/ms-safety/datasetImpl/searchbydatasetimpl" ,json_s.toString());
            for(int i=0;i<JSONArray.fromObject(result).size();i++){
                JSONObject json=(JSONObject)JSONArray.fromObject(result).get(i);
                json.put("czr",json.get("userAccount").toString());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }
    /**
     * 生成生成任务专用
     * 操作人模糊查询
     * @param map
     * @param session
     * @return
     */
    @PostMapping(value="/searchRwCzr")
    @ApiOperation(value = "生成上架任务管理 操作人模糊查询", response = JSONObject.class, notes = "查询输入{\"account\":\"账户类型(0:全部账户;1:当前登陆帐户)\",\"czr\":\"操作人模糊查询\"}")
    public @ResponseBody
    JSONArray searchRwCzr(@RequestBody Map<String, Object> map, HttpSession session) {
        JSONObject json_s = new JSONObject();
        JSONArray jsonArray=new JSONArray();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String hzid=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                hzid=hz.get(0).getHzid();
            }
            //账户类型(0:全部账户;1:当前登陆帐户)
            String account=map.get("account").toString();
            if("0".equals(account)){
                json_s.put("code","hz");
                json_s.put("dvalue",hzid);
                json_s.put("userAccount",map.get("czr").toString());
                String host = PropertyUtil.getProperty("safety.host");
                String port = PropertyUtil.getProperty("safety.port");
                String result = HttpUtil.doPost("http://"+host+":"+port+"/ms-safety/datasetImpl/searchbydatasetimpl" ,json_s.toString());
                for(int i=0;i<JSONArray.fromObject(result).size();i++){
                    JSONObject json=(JSONObject)JSONArray.fromObject(result).get(i);
                    json.put("czr",json.get("userAccount").toString());
                    jsonArray.add(json);
                }
            }else{
                List<WmsShelves> list = baseService.searchRwCzr(map.get("czr").toString(), ckid, hzid);
                for (WmsShelves wmsShelves : list) {
                    JSONObject json = new JSONObject();
                    json.put("czr", wmsShelves.getCzr());
                    jsonArray.add(json);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }
    /**
     * 上架管理 新增上架任务
     * @param list
     * @param session
     * @return
     */
    @PostMapping(value = "/addRW")
    @ApiOperation(value = "上架管理 新增上架任务信息",response = JSONObject.class, notes = "POST新增上架任务信息")
    public @ResponseBody JSONObject addRW(@RequestBody List<WmsShelves> list, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            //取出当前用户
            User user = userinfo.getUser();
            String hzid=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                hzid=hz.get(0).getHzid();
            }
            baseService.addRW(list, ckid, hzid, user);
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增上架任务成功","", JSONArray.fromObject(list).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"新增上架任务失败",e.getMessage(),"");
        } catch (RuntimeException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "新增上架信息失败", e.getMessage(), "");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "新增上架任务失败", e.getMessage(), "");
        }
        return json;
    }
    /**
     *查看任务详情页面查询
     * @param entity
     * @return
     */
    @PostMapping(value = "/selectRw")
    @ApiOperation(value = "上架管理 查看任务详情页面查询",response = JSONObject.class, notes = "POST查看任务详情页面查询")
    public @ResponseBody JSONObject selectRw(@RequestBody WmsShelves entity, HttpSession session){
        Page<WmsShelves> list = new Page<WmsShelves>();
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckmc= String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            entity.setCkid(ckid);
            String owner=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
                entity.setHzid(owner);
                list=baseService.selectRw(entity);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (ServiceException e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"任务详情页面查询失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
